//
//  ChaKanYiXuanChengYanView.m
//  JuBoCRM
//
//  Created by iMac on 2024/2/17.
//  Copyright © 2024 iningKe. All rights reserved.
//

#import "ChaKanYiXuanChengYanView.h"

@implementation ChaKanYiXuanChengYanView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)awakeFromNib{
    [super awakeFromNib];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    
    [_mainTableView registerNib:[UINib nibWithNibName:@"ChaKanYiXuanChengYanViewTableViewCell" bundle:nil] forCellReuseIdentifier:@"ChaKanYiXuanChengYanViewTableViewCell"];
}


- (void)setFirstArray:(NSMutableArray *)firstArray{
    _firstArray = firstArray;
    [_mainTableView reloadData];
}

- (void)setSecondArray:(NSMutableArray *)secondArray{
    _secondArray = secondArray;
    [_mainTableView reloadData];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0){
        return _firstArray.count;
    }
    return _secondArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ChaKanYiXuanChengYanViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChaKanYiXuanChengYanViewTableViewCell"];
    if(indexPath.section == 0){
        NSDictionary *dataDic = _firstArray[indexPath.row];
        
        if(_gongHaiChaKanBottomView.hidden == NO){
            cell.nameLabel.text = [NSString stringWithFormat:@"%@",GetHandleString(dataDic, @"customerName")];
        }else{
            cell.nameLabel.text = [NSString stringWithFormat:@"%@（%@人）",GetHandleString(dataDic, @"name"),GetHandleString(dataDic, @"currentNum")];
        }
        
        cell.removeClickBlock = ^{
            [_firstArray removeObjectAtIndex:indexPath.row];
            
            if(_gongHaiChaKanBottomView.hidden == NO){
                _titleLabel.text = [NSString stringWithFormat:@"已选择：%ld人",_firstArray.count];
                if(_removeObjectBlock){
                    _removeObjectBlock(dataDic);
                }
                _gongHaiNumLabel.text = _titleLabel.text;
            }else{
                _titleLabel.text = [NSString stringWithFormat:@"已选择：%ld人，%ld部门",_firstArray.count,_secondArray.count];
                if(_removeObjectClickBlock){
                    _removeObjectClickBlock(_firstArray,YES);
                }
                _shaiXuanYiXuanNumLabel.text = _titleLabel.text;
            }
        
            
            
            
            [_mainTableView reloadData];
        };
    }else{
        NSDictionary *dataDic = _secondArray[indexPath.row];
        cell.nameLabel.text = [NSString stringWithFormat:@"%@（%@）",GetHandleString(dataDic, @"realname"),GetHandleString(dataDic, @"deptName")];
        cell.removeClickBlock = ^{
            [_secondArray removeObjectAtIndex:indexPath.row];
            
            _titleLabel.text = [NSString stringWithFormat:@"已选择：%ld人，%ld部门",_firstArray.count,_secondArray.count];
            _shaiXuanYiXuanNumLabel.text = _titleLabel.text;
            
            if(_removeObjectClickBlock){
                _removeObjectClickBlock(_secondArray,NO);
            }
            [_mainTableView reloadData];
        };
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (IBAction)guanBi:(UIButton *)sender {
    if(_manageClickBlock){
        _manageClickBlock(sender.tag);
    }
}


@end
